package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.core.exception.ParameterException;
import com.demo.dao.UserDTODao;
import com.demo.dao.UserDao;
import com.demo.domain.User;
import com.demo.domain.UserDTO;
import com.demo.service.MailService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class MailServiceImpl extends ServiceImpl<UserDTODao, UserDTO> implements MailService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private UserDTODao userDTODao;

    @Autowired
    private UserDao userDao;

    @Value("${spring.mail.username}")
    private String fromMail;

    //    给前端输入的邮箱发送验证码
    @Override
    public Object[] sendMail(String email, HttpSession session) throws ParameterException {
        if (email.equals("") || email == null) {
            throw new ParameterException("email为null");
        }
        if (session == null) {
            throw new ParameterException("session为空");
        }
        if (!email.contains("@")) {
            throw new ParameterException("email值非法");
        }
        //检查当前邮箱所属的账号是否仍存在
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUMail,email).eq(User::getUAlive,1);
        if (userDao.selectCount(wrapper)!=1l){
            return null;
        }
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("邮件验证码");
        // 生成随机数
        String code = randomCode();
        session.setAttribute("email", email);
        session.setAttribute("code", code);
        mailMessage.setText("您收到的验证码是:" + code);
        // 发送对象
        mailMessage.setTo(email);
        mailMessage.setFrom(fromMail);
        //发送邮件
        mailSender.send(mailMessage);
        System.out.println(code);
        return new Object[]{email, code, "邮件发送成功"};
    }

    // 生成6位随机数作验证码
    @Override
    public String randomCode() {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            stringBuilder.append(random.nextInt(10));
        }
        return stringBuilder.toString();
    }

    @Override
    public boolean validateCode(UserDTO userDTO, HttpSession session) throws ParameterException {
        if (userDTO == null) {
            throw new ParameterException("userDTO为null");
        }
        if (session == null) {
            throw new ParameterException("session为null");
        }
        // 获取session中的验证信息
        String email = (String) session.getAttribute("email");
        String code = (String) session.getAttribute("code");
        // 获取表单中的提交信息
        String dtoCode = userDTO.getCode();
        //如果email数据为空，或者不一致，注册失败
        if (email == null || email.isEmpty()) {
            System.out.println("email为空");
            return false;
        } else if (!code.equals(dtoCode)) {
            System.out.println("验证码不一致");
            return false;
        }
        return true;
    }
}
